package com.example.base.security.domain;

import com.alibaba.fastjson.annotation.JSONField;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.example.base.security.service.SysDeptService;
import com.example.base.security.service.SysRoleService;
import com.example.base.utils.SpringContextUtils;
import lombok.Data;

import java.io.Serializable;

/**
 * @author Xiongx
 * @version 1.0
 * @date 2021/6/11 14:37
 * @since JDK 1.8
 */
@Data
@TableName("sys_user")
public class SysUserEntity extends DataBase implements Serializable {

    private static final long serialVersionUID = 1L;
    /**
     * 用户ID
     */
    @TableId(type = IdType.AUTO)
    private Integer userId;
    /**
     * 用户名
     */
    private String username;
    /**
     * 密码
     */
    @JSONField(serialize = false, deserialize = false)
    private String password;
    /**
     * 状态:NORMAL正常  PROHIBIT禁用
     */
    private String status= "NORMAL";

    /**
     * 用户姓名
     */
    private String realname;

    /**
     * 用户手机号
     */
  //  @JSONField(serializeUsing = PhoneSensitiveInfoSerializer.class)
    private String phone;

    /**
     * 用户地址
     */
    private String address;

    /**
     * 部门id
     */
    private Integer deptId;

    /**
     * 部门名称
     */
    @TableField(exist = false)
    private SysDept dept;

    /**
     * 角色id
     */
    private Integer roleId;

    /**
     * 用户角色
     */
    @TableField(exist = false)
    private SysRoleEntity sysRoleEntity;

    /**
     * 登录失败次数
     */
    private Integer loginFailNum = 0;

    /**
     * 登录总次数
     */
    private Integer loginNum = 0;

    /**
     * 用户邮箱
     */
    private String userMail;


    public SysDept getDept() {
        if(this.deptId!=null){
            SysDeptService service=SpringContextUtils.getBean(SysDeptService.class);
            this.dept=service.getById(this.deptId);
        }
        return dept;
    }

    public SysRoleEntity getSysRoleEntity() {
        if(this.roleId!=null){
            SysRoleService sysRoleService=SpringContextUtils.getBean(SysRoleService.class);
            this.sysRoleEntity=sysRoleService.getById(this.roleId);
        }
        return sysRoleEntity;
    }
}
